import {
  MessageBox
} from 'element-ui'
//import store from "../store"
import {
  FILE_SERVER
} from '@/store/yg/common/constant';

// ====页面动态加载C-Lodop云打印必须的文件CLodopfuncs.js====
var head = document.head || document.getElementsByTagName('head')[0] || document.documentElement
var oscript = document.createElement('script')
// 让本机的浏览器打印(更优先一点)：
oscript = document.createElement('script')
oscript.src = 'http://localhost:8000/CLodopfuncs.js?priority=2'
head.insertBefore(oscript, head.firstChild)
// 加载双端口(8000和18000）避免其中某个端口被占用：
oscript = document.createElement('script')
oscript.src = 'http://localhost:18000/CLodopfuncs.js?priority=1'
head.insertBefore(oscript, head.firstChild)

// 下载loadLodop
function loadLodop() {
  window.open('http://www.c-lodop.com/download/Lodop6.226_Clodop4.127.zip')
}

// ====获取LODOP对象的主过程：====
export function getLodop() {
  var LODOP
  try {
    LODOP = getCLodop()
    if (!LODOP && document.readyState !== 'complete') {
      MessageBox.alert('C-Lodop打印控件还没准备好，请稍后再试！')
      return
    }
    //LODOP.SET_LICENSES("THIRD LICENSE","","Shenzhen Tomtop Technology Co.,Ltd","74FB8E1DA8AB29DD3D495E6877FD16D7");
    //LODOP.SET_LICENSES("深圳市通拓科技有限公司","BBCE1BD5AB0D52274607D05CDE377965","深圳市通拓科技有限公司","D534B91DF33111308F14784E8C6C8BEA");
    //LODOP.SET_LICENSES("深圳九方通逊电商物流有限公司","87D00E344DD2E646BE3E58663B969D7C","C94CEE276DB2187AE6B65D56B3FC2848","");
      LODOP.SET_LICENSES("","1C0BDCD5F0F063B4606A717C8A739625286","","");
    return LODOP
  } catch (err) {
    MessageBox({
      title: '温馨提示',
      type: 'warning',
      showCancelButton: true,
      message: '您还未安装打印控件，点击确定下载打印控件，安装成功后刷新页面即可进行打印',
      callback: res => {
        if (res === 'confirm') {
          loadLodop()
        }
      }
    })
  }
}
// 获取打印机列表
let getPrinterList = function () {
  const LODOP = getLodop()
  if (LODOP) {
    let count = LODOP.GET_PRINTER_COUNT()
    let printerList = []
    for (let i = 0; i < count; i++) {
      printerList.push({
        name: LODOP.GET_PRINTER_NAME(i),
        id: i
      })
    }
    return printerList
  }
}

// 获取打印机的纸张类型
let getPageSizeList = function (printerIndex) {
  const LODOP = getLodop()
  if (LODOP) {
    let pageSizeStr = LODOP.GET_PAGESIZES_LIST(printerIndex, "\n")
    return pageSizeStr.split("\n");
  }
}

// 打印
let lodopPrint = function (printerConfig,dataOrigin, type) {
  const LODOP = getLodop()
  if (printerConfig == null) {
    //store.commit("updatePrinterDialog", true)
    // 将打印任务数据存起来,配置完打印后打印
    const printTask = {dataOrigin,type}
    localStorage.setItem("printTask",JSON.stringify(printTask))
  } else {

    if (LODOP) {
      LODOP.PRINT_INIT('') //初始化
      LODOP.SET_PRINTER_INDEX(printerConfig.id); // 设置指定打印机
      switch (type) {
        case 'PDF':
          LODOP.ADD_PRINT_PDF(0, 0, "100%", "100%", dataOrigin);
          LODOP.SET_PRINT_STYLEA(0, "PDFScalMode", 1);
          break;
        case 'HTML':
          LODOP.ADD_PRINT_HTML(0, 0, "100%", "100%", dataOrigin);
          break;
        case 'PNG':
          //LODOP.ADD_PRINT_IMAGE(0, 0, "100%", "100%", dataOrigin);
          LODOP.ADD_PRINT_IMAGE(0,0,380,570,dataOrigin);
          LODOP.SET_PRINT_STYLEA(0,"Stretch",1);//(可变形)扩展缩放模式
          break;
        default:
          break;
      }
      LODOP.SET_PRINT_PAGESIZE(1, 0,0,"A4");
      LODOP.PRINT()
    }
  }
}

// 打印图片
let lodopPrintImg = function (printerConfig,dataOrigin, type) {
  const LODOP = getLodop()
  if (LODOP) {
    LODOP.PRINT_INIT('') //初始化
    LODOP.SET_PRINTER_INDEX(printerConfig.id); // 设置指定打印机
    if (printerConfig == null) {
      // 将打印任务数据存起来,配置完打印后打印
      const printTask = {dataOrigin,type}
      localStorage.setItem("printTask",JSON.stringify(printTask))
    } else {
      for (var i=0;i<dataOrigin.length;i++) {
        var dataUrl = FILE_SERVER + dataOrigin[i];
        console.log("print============>"+dataUrl);
        switch (type) {
          case 'PDF':
            LODOP.ADD_PRINT_PDF(0, 0, "100%", "100%", dataUrl);
            LODOP.SET_PRINT_STYLEA(0, "PDFScalMode", 1);
            break;
          case 'HTML':
            LODOP.ADD_PRINT_HTML(0, 0, "100%", "100%", dataUrl);
            break;
          case 'PNG':
            LODOP.ADD_PRINT_IMAGE(0,0,380,570,"<img border='0' src='" + dataUrl + "' />");
            LODOP.SET_PRINT_STYLEA(0,"Stretch",1);//(可变形)扩展缩放模式
            break;
          default:
            break;
        }
        LODOP.NewPage()
        LODOP.SET_PRINT_PAGESIZE(1, 100, 0,"A4");
        LODOP.PRINT();
      }
    }
  }
}

export {
  lodopPrint,
  getPrinterList,
  getPageSizeList,
  lodopPrintImg
}
